﻿/*
--------------------------------------
File: CropEdges.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A algorithm which crops the image edges, best to use after rotation
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class CropEdges : VisionAlgorithm
    {
        public CropEdges(String name)
            : base(name)
        {
            hasConfiguration = false;
        }
 
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            int[] sourceArray = MyImage.BmpToArray(sourceImage);
            int width = sourceImage.Width;
            
            //The dimensions
            int left_top = 0,
                right_top = width - 1, 
                left_bottom = (sourceImage.Height - 1) * width, 
                right_bottom = sourceImage.Height * width - 1;

            //Resets all the static class variables used in BlobUtils
            BlobUtils.initialize();
            
            //Filters the top row
            for (int i = left_top; i <= right_top; i++)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, -16777216, -1, width, i);
            }
            //filter the bottom row
            for (int i = left_bottom; i <= right_bottom; i++)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, -16777216, -1, width, i);
            }
            //Filter first column
            for (int i = left_top; i <= left_bottom; i += width)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, -16777216, -1, width, i);
            }
            //Filter last column
            for (int i = right_top; i <= right_bottom; i += width)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, -16777216, -1, width, i);
            }            

            BlobUtils.initialize();
            BlobUtils.findBlobs(ref sourceArray, -16777216, width);
            BlobUtils.filterSmallBlobs(sourceArray.Length * 0.001);
            //BlobUtils.filterBlobs(ref sourceArray);

            //For outputing the characters as Bitmap images
            List<Bitmap> characters = BlobUtils.getCharacters(ref sourceArray, width);
            int counter = 0;
            foreach (Bitmap b in characters)
            {
                //b.Save("c:\\test1\\" + counter + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                counter++;
            }

            //Gets the result op blob detection
            Bitmap returnBitmap = new Bitmap(sourceImage);

            returnBitmap = MyImage.ArrayToBmp(sourceArray, returnBitmap);
            return returnBitmap;
        }
    }
}






